﻿@using MudBlazor.Services
@namespace MudBlazor.Docs.Examples
@implements IDisposable

<MudCard Class="pa-5">
    <MudText>
        Resize the window and see width and height change:<br />
        Browser window is @(width)x@(height)px
    </MudText>
</MudCard>

@code
{
    [Inject] IResizeListenerService ResizeListener { get; set; }

    int width = 0;
    int height = 0;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            ResizeListener.OnResized += OnResized;

            var size = await ResizeListener.GetBrowserWindowSize();
            height = size.Height;
            width = size.Width;
            StateHasChanged();
        }

        await base.OnAfterRenderAsync(firstRender);
    }

    private void OnResized(object sender, BrowserWindowSize size)
    {
        width = size.Width;
        height = size.Height;
        InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        ResizeListener.OnResized -= OnResized;
    }
}
